<?php

namespace App\Admin\Http\Controllers\Web;

use App\Admin\Http\Requests\ArticleCategoryRequest;
use Flash;
use App\Admin\Eloquent\ArticleCategory;
use App\Admin\Http\Controllers\Controller;
use App\Extensions\Pagination\BootstrapThreePresenter;
use Illuminate\Http\Request;

class ArticleCategoriesController extends Controller
{
  /**文章列表
   * @param Request $request
   * @param int $per_page
   * @return \Illuminate\Http\Response
   */
  public function index(Request $request)
  {
    $article_category_name = $request->input('article_category_name');
    $article_category_parent_name = $request->input('article_category_parent_name');
    $query = ArticleCategory::query();

    //判断查询父级 默认为0
    $parent = $request->input('parent');
    if(!empty($parent)){
      $query = $query->where('parent', $parent);
    }else{
      $query = $query->where('parent', 0);
    }

    if(!empty($article_category_parent_name)){
      $parent_query =ArticleCategory::where('name',$article_category_parent_name)->first();
      if(!empty($parent_query)){
        $query=$query->where('parent','=',$parent_query->id);
      }
    }

    if (!empty($article_category_name))
      $query = $query->where('name', 'like', '%' . $article_category_name . '%');

    $article_categories = $query->orderBy('id', 'desc')->paginate(15);
    if (!empty($article_category_name))
      $article_categories->appends(['article_category_name' => $article_category_name]);

    if($article_categories->total() == 0){
      Flash::error('没有找到子级文章分类!');

      return back()->withInput();    
    }

      // 分页
    $links = (new BootstrapThreePresenter($article_categories))->render();

    return view('admin.web.article_categories.index', compact('article_categories', 'article_category_name','article_category_parent_name','links'));
  }

  /**
   * 新增文章表单
   *
   * @return \Illuminate\Http\Response
   */
  public function create()
  {
    $article_category = ArticleCategory::lists('id', 'name');

    return view('admin.web.article_categories.create', compact('article_category'));
  }

  /**
   * 新增文章分类表单
   *
   * @param ArticleCategoryRequest $request
   * @return \Illuminate\Http\RedirectResponse
   */
  public function store(ArticleCategoryRequest $request)
  {
    $article_category = new ArticleCategory($request->input());
    $article_category->save();

    Flash::success('新增文章分类成功');

    return $this->redirectAction('Web\ArticleCategoriesController@index');
  }

  /**
   * 根据id删除文章分类
   * @param int $id
   * @return \Illuminate\Http\Response
   */
  public function destroy($id)
  {
    $article_category = ArticleCategory::findOrFail($id);

    $article_category->delete();

    Flash::success('删除文章分类成功');

    return $this->redirectAction('Web\ArticleCategoriesController@index');
   }

  /**
   * 编辑文章分类表单
   * @param int $id
   * @return \Illuminate\Http\Response
   */
  public function edit(int $id)
  {
    $article_category = ArticleCategory::findOrFail($id);

    $parents = ArticleCategory::where('id', '!=', $id)
            ->where('parent', '!=', $id)
            ->lists('id', 'name');

    return view('admin.web.article_categories.edit', compact('article_category', 'parents'));
  }

  /**
   * 修改文章分类表单
   * @param ArticleCategoryRequest $request
   * @param int $id
   * @return \Illuminate\Http\Response
   * @throws BusinessException
   */
  public function update(ArticleCategoryRequest $request, int $id)
  {
    $arr = $request->input();
    ArticleCategory::findOrFail($id)->update($arr);

    Flash::success('修改文章分类成功');

    return $this->redirectAction('Web\ArticleCategoriesController@index');
  }

  /**
   * 根据id查看文章分类
   * @param int $id
   * @return \Illuminate\Http\Response
   */
  public function show(int $id)
  {
    $article_category = ArticleCategory::findOrFail($id);

    $parent = ArticleCategory::where('id', $article_category->parent)->value('name');

    return view('admin.web.article_categories.show', compact('article_category', 'parent'));
  }
}


